﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MechanicCalculator.Utility
{
    class TrialTimeManager
    {
        /// <summary>
        /// A Temporary variable.
        /// </summary>
        private string temp = "";
        SerialDialog serialDialog = new SerialDialog();
        /// <summary>
        /// The constructor.
        /// </summary>
        public TrialTimeManager()
        {

        }

        /// <summary>
        /// Sets the new date +31 days add for trial.
        /// </summary>
        public void SetNewDate()
        {
            DateTime newDate = DateTime.Now.AddDays(GlobalConstants.NUMBER_OF_DAYS);
            temp = newDate.ToLongDateString();
            StoreDate(temp);
        }

        /// <summary>
        /// Checks if expire or NOT.
        /// </summary>
        public GlobalConstants.USAGE_TYPE Expired()
        {
            string d = "";
            using (Microsoft.Win32.RegistryKey key =
                Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Test"))
            {
                d = (String)key.GetValue("Date");
                if (d == "-1")
                {

                    return GlobalConstants.USAGE_TYPE.FULL;
                }
            }
            DateTime now = DateTime.Parse(d);
            int day = (now.Subtract(DateTime.Now)).Days;


            if (day > GlobalConstants.NUMBER_OF_VALID_USAGE)
            {
                return GlobalConstants.USAGE_TYPE.FULL;
            }
            else if (0 < day && day <= GlobalConstants.NUMBER_OF_VALID_USAGE)
            {
                string daysLeft = string.Format("{0} days more to expire.", now.Subtract(DateTime.Now).Days);
                MessageBox.Show(daysLeft, "Oops", MessageBoxButtons.OK);
                return GlobalConstants.USAGE_TYPE.LIMITED;
            }
            else if (day <= 0)
            {
                /* Fill with more code, once it has expired, what will happen nex! */
                DialogResult r = MessageBox.Show("Your evaluation period has expired. Please register for a full version.", "Oops", MessageBoxButtons.YesNo);
                if (r == DialogResult.Yes)
                {
                    DialogResult serialResult = serialDialog.ShowDialog();
                    if (serialResult == DialogResult.OK)
                    {
                        new Thread(new ThreadStart(delegate
                        {
                            MessageBox.Show("Successfully registered", "WOW");
                        })).Start();
                        using (Microsoft.Win32.RegistryKey key =
                    Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Test"))
                        {
                            key.SetValue("Date", "-1", Microsoft.Win32.RegistryValueKind.String);
                        }
                        
                        return GlobalConstants.USAGE_TYPE.FULL;
                    }
                    else
                    {
                        MessageBox.Show("Wrong serial information. Please register for a correct one. See ya");
                    }
                } 
            }
            return GlobalConstants.USAGE_TYPE.EXIT;
        }

        /// <summary>
        /// Stores the new date value in registry.
        /// </summary>
        /// <param name="value"></param>
        private void StoreDate(string value)
        {
            try
            {
                using (Microsoft.Win32.RegistryKey key =
                    Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Test"))
                {
                    if (key.GetValue("Date") == null)
                    {
                        key.SetValue("Date", value, Microsoft.Win32.RegistryValueKind.String);
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
